package com.ygj.duty.service.impl;

import com.base.common.ConfigConstants;
import com.base.common.enums.TicketCategories;
import com.base.common.enums.TicketReportCategories;
import com.base.common.enums.TicketReportStatus;
import com.base.common.enums.TicketStatus;
import com.base.common.persistent.dao.*;
import com.base.common.persistent.model.*;
import com.base.common.persistent.query.TDutyAdjustQuery;
import com.base.common.persistent.query.ViewDuty01Query;
import com.base.common.utils.EasyUtil;
import com.ygj.duty.service.DutyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.*;

/**
 * Created by Kevin on 2017-7-13.
 */
@Service("dutyService")
public class DutyServiceImpl implements DutyService{

    @Resource
    private ViewDuty01Mapper viewDuty01Mapper;

    @Resource
    private TDutyMapper tDutyMapper;

    @Resource
    private TDutyAdjustMapper tDutyAdjustMapper;

    @Resource
    private TicketMapper ticketMapper;

    @Resource
    private TicketReportMapper ticketReportMapper;

    @Resource
    private EasyUtil easyUtil;

    @Resource
    private EmployeeCompoundsMapper employeeCompoundsMapper;

    @Resource
    private VillageConfigMapper villageConfigMapper;

    @Override
    public Object queryDutyInfo(int compoundsId, String queryTime) {
        ViewDuty01Query viewDuty01Query = new ViewDuty01Query();
        viewDuty01Query.setDutyDate(queryTime);
        viewDuty01Query.setCompoundsId(compoundsId);
        return viewDuty01Mapper.selectByCondition(viewDuty01Query);
    }

    @Override
    public Object queryDutyPlan(int employeeId) {
        return tDutyMapper.selectByEmployeeId(employeeId);
    }

    @Override
    @Transactional
    public Object updateDutyPlan(String remarks, int user_id1, int user_id2, int dutyId, int compoundsId) {
        TDutyAdjust tDutyAdjust = new TDutyAdjust();
        tDutyAdjust.setUserId1(user_id1);
        tDutyAdjust.setUserId2(user_id2);
        tDutyAdjust.setPlanId(dutyId);
        tDutyAdjust.setRemarks(remarks);
        tDutyAdjust.setSubmitTime(new Date());
        tDutyAdjust.setStatus(0);
        String dutyAdjustCode = getDutyAdjustCode();
        tDutyAdjust.setAdjustCode(dutyAdjustCode);
        tDutyAdjustMapper.insertSelective(tDutyAdjust);

        Ticket ticket = new Ticket();
        ticket.setGetTime(easyUtil.getDateStrByGeneral(new Date()));
        ticket.setCategories(TicketCategories.DUTY.getValue());
        ticket.setStatus(TicketStatus.EXECUTION.getValue());
        ticket.setContext(remarks);
        int employeeCompoundsId = employeeCompoundsMapper
                .queryEmployeeCompoundsIdByEmployeeIdAndCompoundsId(user_id1, compoundsId);
        if (employeeCompoundsId == 0) {
            return null;
        }
        ticket.setEmployeeCompoundsId(employeeCompoundsId);

        // 设置工单号，查询最新工单号，并在基础上加1
        List<Ticket> tickets = new ArrayList<Ticket>();
        String code = getDutyTicketCode(tickets, TicketCategories.DUTY.getName(),
                TicketCategories.DUTY.getValue());
        ticket.setCode(code);
        ticket.setSubclass(new Subclass().setId(villageConfigMapper.selectSubClassID(compoundsId)));
        ticket.setSubCode(String.valueOf(tDutyAdjustMapper.selectIdByCode(dutyAdjustCode)));
        ticketMapper.addTicket(ticket);

        TicketReport ticketReport = new TicketReport();
        ticketReport.setStartTime(new Date());
        ticketReport.setEmployee(new Employee().setId(villageConfigMapper.queryDutyApprovalEmployeeId(compoundsId)));
        ticketReport.setTicket(new Ticket().setId(ticketMapper.queryTicketByCode(code).getId()));
        ticketReport.setCategories(TicketReportCategories.EXECUTION.getValue());
        ticketReport.setStatus(TicketReportStatus.UNTREATED.getValue());
        ticketReportMapper.addTicketReport(ticketReport);
        return 1;
    }

    /**
     * 生成adjust code
     *
     * @return
     */
    private String getDutyAdjustCode() {
        String lastCode = null;
        try {
            lastCode = tDutyAdjustMapper.queryLastCode();
        } catch (Exception e) {
            return null;
        }
        String code = "";
        DecimalFormat df = new DecimalFormat(ConfigConstants.TICKET_CODE_NUMS);
        if (lastCode != null && !"".equals(lastCode)) {
            System.out.println(lastCode);
            code = df.format(Integer.parseInt(lastCode.substring(1).trim()) + 1);
            System.out.println(code);
        } else {
            code = df.format(1);
        }
        code = "T" + code;
        return code;
    }

    /**
     * 生成调班工单的code
     *
     * @param tickets
     * @return
     */
    private String getDutyTicketCode(List<Ticket> tickets, String prefixCode,
                                     int categories) {
        try {
            tickets = ticketMapper.queryLastCodeByCategories(categories);
        } catch (Exception e) {
            return null;
        }
        String code = "";
        DecimalFormat df = new DecimalFormat(ConfigConstants.TICKET_CODE_NUMS);
        if (tickets.size() != 0) {
            String lastedCode = tickets.get(0).getCode();
            code = df.format(Integer.parseInt(lastedCode.substring(1).trim()) + 1);
        } else {
            code = df.format(1);
        }
        code = prefixCode + code;
        return code;
    }


}
